/**
 * Copyright 2012 Meruvian
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.yama.action;

import javax.inject.Inject;

import org.meruvian.inca.struts2.rest.ActionResult;
import org.meruvian.inca.struts2.rest.annotation.Action;
import org.meruvian.inca.struts2.rest.annotation.Action.HttpMethod;
import org.meruvian.inca.struts2.rest.annotation.ActionParam;
import org.meruvian.inca.struts2.rest.annotation.Result;
import org.meruvian.inca.struts2.rest.annotation.Results;
import org.meruvian.yama.persistence.EntityListWrapper;
import org.meruvian.yama.persistence.utils.PagingUtils;

import com.example.yama.entity.Category;
import com.example.yama.service.CategoryService;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author Dian Aditya
 * 
 */
@Action(name = "/module/category")
@Results({ @Result(name = "list", type = "redirect", location = "/module/category") })
public class CategoryAction implements ModelDriven<Object> {
	@ActionParam("category")
	private Category category = new Category();

	@ActionParam("limit")
	private int limit = 10;

	@ActionParam("page")
	private int page = 1;

	private EntityListWrapper<Category> categories = new EntityListWrapper<Category>();

	@Inject
	private CategoryService categoryService;

	@Action(name = "/edit/{category.id}", method = HttpMethod.GET)
	public ActionResult edit() {
		category = categoryService.findCategoryById(category.getId());

		return new ActionResult("/view/module/category/category-add.ftl")
				.setType("freemarker");
	}

	@Action(name = "/add", method = HttpMethod.GET)
	public ActionResult form() {
		return new ActionResult("/view/module/category/category-add.ftl")
				.setType("freemarker");
	}

	@Action(name = "/add", method = HttpMethod.POST)
	public String add() {
		if (category.getId() != null) {
			if (category.getId().trim().equalsIgnoreCase(""))
				category.setId(null);
		}
		categoryService.saveCategory(category);

		return "list";
	}

	@Action(method = HttpMethod.DELETE)
	public String delete() {
		categoryService.deleteCategory(category);

		return "list";
	}

	@Action
	public ActionResult execute() {
		if (category == null) {
			category = new Category();
		}

		String name = category.getName() == null ? "" : category.getName();

		categories = categoryService.getCategoriesByName(name, limit, page - 1);
		categories.setTotalPage(PagingUtils.getTotalPage(
				categories.getRowCount(), limit));

		return new ActionResult("/view/module/category/category-list.ftl")
				.setType("freemarker");
	}

	@Action(name = "/{category.id}")
	public ActionResult detail() {
		category = categoryService.findCategoryById(category.getId());

		return new ActionResult("/view/module/category/category-add.ftl")
				.setType("freemarker");
	}

	public Object getModel() {
		return categories.getRowCount() < 1 ? category : categories;
	}
}
